<?php

class WSB_Sitemap
{

	const MODE_DEFAULT = 1;
	const MODE_ADD = 2;
	const MODE_PASTE = 3;
	const MODE_HTML_EDITOR_PLUGIN = 4;

	/**
	 * @var WSB_Table
	 */
	private $_table;

	/**
	 * @var integer
	 */
	private $_mode;

	/**
	 * @var integer
	 */
	private $_idpage;

	/**
	 * @param integer $mode
	 */
	public function __construct($mode=null)
	{
		$this->_mode = isset($mode) ? $mode : self::MODE_DEFAULT ;
	}

	/**
	 * @return void
	 */
	public function display()
	{
    	echo $this->render();
	}

	/**
	 * @return string
	 */
	public function render()
	{

		$translate = Zend_Registry::get('Zend_Translate');
		$locale = Zend_Registry::get('Zend_Locale');
		$translation = WSB_MODULES_PATH . 'cms/etc/page.' . $locale.'.csv';
		$translate->addTranslation($translation, $locale);

		$this->_table = new WSB_Table('class="style-1 style-1-nowrap"');
		$this->_table->setAutoGrow(true);
		$this->_table->setAutoFill('&nbsp;');

		$colgroup = array();
		array_push($colgroup, '');
		if($this->_mode == self::MODE_DEFAULT) {
			array_push($colgroup, 'width="1%"');
			array_push($colgroup, 'width="1%"');
			array_push($colgroup, 'width="1%"');
		}
		array_push($colgroup, 'width="5%"');
		$this->_table->setColGroup($colgroup);

		$columns = array();
		array_push($columns, $translate->_('page:pagetitle'));
		if($this->_mode == self::MODE_DEFAULT) {
			array_push($columns, $translate->_('page:has template'));
			array_push($columns, $translate->_('page:languages'));
			array_push($columns, 'Perm');
		}
		array_push($columns, '');
		$this->_table->addRow($columns, null, 'th');

		// Rekursion
		$this->_goTroughNodeRec(WSB_Node_Page::getTree());

		return $this->_table->render();
	}

	/**
	 * @param integer $page_id
	 */
	public function setIdPage($page_id)
	{
		$this->_idpage = $page_id;
	}

	/**
	 * @param WSB_Node_Page $node
	 * @return string
	 */
	private function _getPageIcon(WSB_Node_Page $node)
	{
		// Inaktive Seite: Keine spezielles Icon, da Seite keine "Funktion" hat
		if(!empty($node->hidden)) {
			return WSB_Icon::pageHidden();
		}
		// Shortcut, Nicht im Menu, Fegroups
		if(!empty($node->shortcut) && !empty($node->nav_hide) && !empty($node->fegroups)) {
			return WSB_Icon::pageShortcutNavhideFegroups();
		}
		// Shortcut, Nicht im Menu
		if(!empty($node->shortcut) && !empty($node->nav_hide)) {
			return WSB_Icon::pageShortcutNavhide();
		}
		// Shortcut, Fegroups
		if(!empty($node->shortcut) && !empty($node->fegroups)) {
			return WSB_Icon::pageShortcutFegroups();
		}
		// Nicht im Menu, Fegroups
		if(!empty($node->nav_hide) && !empty($node->fegroups)) {
			return WSB_Icon::pageNavhideFegroups();
		}
		// Shortcut
		if(!empty($node->shortcut)) {
			return WSB_Icon::pageShortcut();
		}
		// Nicht im Menu
		if(!empty($node->nav_hide)) {
			return WSB_Icon::pageNavhide();
		}
		// Zugriff definiert
		if(!empty($node->fegroups)) {
			return WSB_Icon::pageFegroups();
		}
		return WSB_Icon::page();
	}

	/**
	 * @param WSB_Node_Page $node
	 * @return string
	 */
	private function _getPermissionIcon(WSB_Node_Page $node)
	{
		$recursive = !(boolean)$node->template_not_recursive;
		$title = sprintf("Template ID: %s\n", intval($node->template_id));
		$title .= sprintf("Recursive: %s\n", $recursive ? 'Yes' : 'No');
		return WSB_Icon::help(null, $title);
	}

	/**
	 * @param WSB_Node_Page $node
	 * @return string
	 */
	private function _getTemplateIcon(WSB_Node_Page $node)
	{
		$recursive = !(boolean)$node->template_not_recursive;
		$title = sprintf("Template ID: %s\n", intval($node->template_id));
		$title .= sprintf("Recursive: %s\n", $recursive ? 'Yes' : 'No');
		return WSB_Icon::help(null, $title);
	}

	/**
	 * @param WSB_Node_Page $node
	 */
	private function _goTroughNodeRec(WSB_Node_Page $node)
	{

		$columns = array();
		static $class = 'even';
		static $rowIndex = 0;

		$indent = '';
		for($i=1; $i<=$node->getLevel(); $i++) {
			$indent .= '&nbsp;&nbsp;';
		}
		$indent = sprintf('<span>%s</span>', $indent);

		// Seitenname
		$pagetitle = sprintf('%s', str_replace(' ' , '&nbsp;', $node->title));

		array_push($columns, $indent . $this->_getPageIcon($node) . $pagetitle);

		if($this->_mode == self::MODE_DEFAULT) {
			// Template
			$template = '';
			if(intval($node->template_id) > 0) {
				$template = $this->_getTemplateIcon($node);
			}
			array_push($columns, $template);

			// Sprachen
			array_push($columns, $node->getLanguageVariantsAsString());

			// Fegroups
			$fegroupsRecursive = empty($node->fegroups_recursive) ? '' : '-R';
			array_push($columns, $node->fegroups . $fegroupsRecursive);
		}

		// Links
		$links = array();
		switch($this->_mode) {
			case self::MODE_DEFAULT:
				array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::edit(), Cms_Url::pageEditUrl($node->getPageId())));
				array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::editContent(), Cms_Url::containerEditUrl($node->getPageId())));
				if($node->hidden == 0) {
					array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::visible(), Cms_Url::pageHideUrl($node->getPageId())));
				} else {
					array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::hidden(), Cms_Url::pageUnhideUrl($node->getPageId())));
				}
				if($node->hasParent()) {
					if(!$node->hasChildren()) {
						//array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::delete(), Cms_Url::pageDeleteUrl($node->getPageId(), $node->getParentId())));
						$url = Cms_Url::pageDeleteUrl($node->getPageId(), $node->getParentId());
						array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::delete(), "javascript:wsbYesNoDialog('".$url."','".$node->getId()."');"));
					} else {
						array_push($links, WSB_Icon::cleargif());
					}
				}
				if($node->hasParent()) {
					array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::cut(), Cms_Url::pageCutUrl($node->getPageId())));
				}
				break;
			case self::MODE_ADD:
				if($node->hasParent()) {
					array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::up(), Cms_Url::pageAddBeforeUrl($node->getParentId(), $node->sort)));
					array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::down(), Cms_Url::pageAddBelowUrl($node->getParentId(), $node->sort+1)));
					if(!$node->hasChildren()) {
						array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::child(), Cms_Url::pageAddChildUrl($node->getPageId())));
					}
				} else {
					if(!$node->hasChildren()) {
						array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::child(), Cms_Url::pageAddChildUrl($node->getPageId())));
					}
				}
				break;
			case self::MODE_PASTE:
				if(!WSB_Node_Page::isSubPage($node, $this->_idpage)) {
					if($node->hasParent()) {
						array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::up(), Cms_Url::pagePasteBeforeUrl($this->_idpage, $node->getParentId(), $node->sort)));
						array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::down(), Cms_Url::pagePasteBelowUrl($this->_idpage, $node->getParentId(), $node->sort+1)));
						if(!$node->hasChildren()) {
							array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::child(), Cms_Url::pagePasteChildUrl($this->_idpage, $node->getPageId())));
						}
					} else {
						if(!$node->hasChildren()) {
							array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::child(), Cms_Url::pagePasteChildUrl($this->_idpage, $node->getPageId())));
						}
					}
				}
				break;
			case self::MODE_HTML_EDITOR_PLUGIN:
					$link = sprintf('<a href="index.php?id=%d">%s</a>', $node->getPageId(), $node->title);
					$link = htmlspecialchars($link);
					array_push($links, WSB_Controller_Url::getHtmlLink(WSB_Icon::add(), "javascript:insertHtml('$link');"));
				break;
		}
		array_push($columns, implode('', $links));
		$this->_table->addRow($columns, array('class' => $class), 'td', true);
		$class = ($class == 'odd') ? 'even' : 'odd';

		foreach($node->getChildren() AS $child) {
			$rowIndex++;
			$this->_goTroughNodeRec($child);
		}

	}

}
